// DB4o.cs
//
//  Copyright (C) 2008 Whitemice Consulting
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
//

using System;
using System.IO;
using Db4objects.Db4o;
using Db4objects.Db4o.Config;
using Db4objects.Db4o.Ext;
using Db4objects.Db4o.IO;
using Db4objects.Db4o.Messaging;

namespace Whitemice.ZOGI.Backend
{
  public class DB4o : IMessageRecipient
  {
    private IObjectContainer          container;
    private static IConfiguration     configuration;
    private MemoryIoAdapter           adapter;
    
    public DB4o(bool init)
    {
      configuration = Db4oFactory.NewConfiguration();
      configuration.ActivationDepth(3);
      configuration.WeakReferences(false);
      configuration.BlockSize(8);
      configuration.ObjectClass(typeof(Whitemice.ZOGI.Entity)).CascadeOnUpdate(true);
      configuration.ObjectClass(typeof(Whitemice.ZOGI.Entity)).ObjectField("_parentObjectId").Indexed(true);
      configuration.ObjectClass(typeof(Whitemice.ZOGI.Entity)).ObjectField("_version").Indexed(true);
      configuration.ObjectClass(typeof(Whitemice.ZOGI.Entity)).ObjectField("_entityName").Indexed(true);
      configuration.ObjectClass(typeof(Whitemice.ZOGI.CompanyValue)).ObjectField("_name").Indexed(true);
      adapter = new MemoryIoAdapter();
      adapter.GrowBy(1000000);
      configuration.Io(adapter);
      container = Db4oFactory.OpenFile(configuration, "ZOGI.DB4o");            
    }
    
    public IObjectContainer Container { get { return container; } }
    
    public void Stop()
    {
      container.Close();
    }
    
    #region IMessageRecipient Members

    public void ProcessMessage(IMessageContext con, object message)
    {
      Console.WriteLine(message);
    }

    #endregion
  } // end DB4o}}
}